/**
 * 🎯 等级识别工具类
 * 统一管理用户等级计算逻辑
 * 
 * 等级规则：
 * - >= 3000: 玩神 (level 4)
 * - >= 1200: 大玩家 (level 3)
 * - >= 600:  资深玩家 (level 2)
 * - >= 300:  会玩儿 (level 1)
 * - < 300:   刚玩儿 (level 0)
 */

/**
 * 根据积分计算等级
 * @param {number} points - 用户积分
 * @returns {number} 等级编号 (0-4)
 */
function calculateLevelFromPoints(points) {
  if (!points || points < 0) {
    return 0; // 刚玩儿
  }
  
  if (points >= 3000) {
    return 4; // 玩神
  } else if (points >= 1200) {
    return 3; // 大玩家
  } else if (points >= 600) {
    return 2; // 资深玩家
  } else if (points >= 300) {
    return 1; // 会玩儿
  } else {
    return 0; // 刚玩儿
  }
}

/**
 * 根据积分返回等级名称
 * @param {number} points - 用户积分
 * @returns {string} 等级名称
 */
function getLevelNameFromPoints(points) {
  const level = calculateLevelFromPoints(points);
  
  switch (level) {
    case 0: return '刚玩儿';
    case 1: return '会玩儿';
    case 2: return '资深玩家';
    case 3: return '大玩家';
    case 4: return '玩神';
    default: return '刚玩儿';
  }
}

/**
 * 获取所有等级配置
 * @returns {Array} 等级配置数组
 */
function getAllLevels() {
  return [
    { id: 1, name: '刚玩儿', points: '0~299', minPoints: 0 },
    { id: 2, name: '会玩儿', points: '300~599', minPoints: 300 },
    { id: 3, name: '资深玩家', points: '600~1199', minPoints: 600 },
    { id: 4, name: '大玩家', points: '1200~2999', minPoints: 1200 },
    { id: 5, name: '玩神', points: '3000+', minPoints: 3000 }
  ];
}

/**
 * 获取等级特权描述
 * @param {string} levelName - 等级名称
 * @returns {string} 特权描述
 */
function getLevelPrivilege(levelName) {
  const privileges = {
    '刚玩儿': '无',
    '会玩儿': '好友申请',
    '资深玩家': '免费好友申请3次/天+免费发布活动1次/天',
    '大玩家': '免费好友申请5次/天+免费发布活动2次/天',
    '玩神': '专属头像框+免费好友申请10次/天+免费发布活动5次/天'
  };
  
  return privileges[levelName] || '无';
}

/**
 * 根据积分获取等级进度信息
 * @param {number} points - 当前积分
 * @returns {Object} { currentLevel, currentLevelName, nextLevel, nextLevelName, progress, pointsToNext }
 */
function getLevelProgress(points) {
  const currentLevel = calculateLevelFromPoints(points);
  const currentLevelName = getLevelNameFromPoints(points);
  const levels = getAllLevels();
  
  // 查找下一个等级
  const nextLevelConfig = levels.find(level => level.minPoints > points);
  
  if (!nextLevelConfig) {
    // 已经是最高等级
    return {
      currentLevel,
      currentLevelName,
      nextLevel: null,
      nextLevelName: null,
      progress: 100,
      pointsToNext: 0,
      isMaxLevel: true
    };
  }
  
  // 查找当前等级的起始积分
  const currentLevelConfig = levels.find(level => level.minPoints <= points) || levels[0];
  const currentLevelStart = currentLevelConfig.minPoints;
  const nextLevelStart = nextLevelConfig.minPoints;
  
  // 计算进度百分比
  const pointsInCurrentLevel = points - currentLevelStart;
  const pointsNeededForNextLevel = nextLevelStart - currentLevelStart;
  const progress = Math.min(100, Math.floor((pointsInCurrentLevel / pointsNeededForNextLevel) * 100));
  const pointsToNext = nextLevelStart - points;
  
  return {
    currentLevel,
    currentLevelName,
    nextLevel: nextLevelConfig.id - 1,
    nextLevelName: nextLevelConfig.name,
    progress,
    pointsToNext,
    isMaxLevel: false
  };
}

module.exports = {
  calculateLevelFromPoints,
  getLevelNameFromPoints,
  getAllLevels,
  getLevelPrivilege,
  getLevelProgress
};

